/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package topcoder;

/**
 *
 * @author Mohamed.Azouz
 */
public class TheEquation {

    public int leastSum(int X, int Y, int P) {
        int last = -1;
        for (int i = 1; i <= P; i++) {
            for (int j = 1; j <= P; j++) {
                int eq = i * X + j * Y;
                System.out.println(i + "  " +  j +  "  "  + eq);
                if (eq % P == 0 ) {
                    System.out.println("Winner is " +  i + "   "  + j);
                    return i + j;
                }
            }
        }
        return last;
    }

    public static void main(String[] args) {
        TheEquation t = new TheEquation();
        int X = 998;
        int Y = 999;
        int P = 1000;
        System.out.println(t.leastSum(X, Y, P));
    }
}
